{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "28376801",
   "metadata": {},
   "source": [
    "### 网络通讯架构\n",
    "设备端  ->  云服务器  -> 数据接收端\n",
    "\n",
    "### 好奇心服务器\n",
    "http://120.26.43.1:5000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eecc38c4",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "134785e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"temp\": 22, \"RunTime\": [2000, 1, 1, 0, 49, 58], \"freq\": \"10s\", \"Dev\": \"HQX_Wheather_Test03\", \"hum\": 53}\n"
     ]
    }
   ],
   "source": [
    "#导入库\n",
    "import requests as r\n",
    "import psutil\n",
    "\n",
    "#读取数据\n",
    "cpu = psutil.cpu_percent()\n",
    "url = \"http://120.26.43.1:5000\"\n",
    "\n",
    "#连接服务器，获取数据\n",
    "d = r.get(url).text\n",
    "\n",
    "#输出\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c3d3006",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入库\n",
    "import requests as r\n",
    "import psutil\n",
    "\n",
    "#读取数据\n",
    "cpu = psutil.cpu_percent()\n",
    "url = \"http://120.26.43.1:5000\"\n",
    "\n",
    "#连接服务器\n",
    "r.get(url,json=cpu)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24d913e0",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f58b1b37",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Time:22-12-11 20:33:36  CPU_Data:1.5  MEM_Data:32.0\n",
      "Time:22-12-11 20:33:37  CPU_Data:0.4  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:38  CPU_Data:1.4  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:39  CPU_Data:1.0  MEM_Data:32.0\n",
      "Time:22-12-11 20:33:40  CPU_Data:1.4  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:41  CPU_Data:1.8  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:42  CPU_Data:14.9  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:43  CPU_Data:0.0  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:44  CPU_Data:2.3  MEM_Data:31.9\n",
      "Time:22-12-11 20:33:45  CPU_Data:0.6  MEM_Data:31.9\n"
     ]
    }
   ],
   "source": [
    "#导入库\n",
    "import psutil,time\n",
    "import requests as r \n",
    "\n",
    "for i in range(10):\n",
    "    #等待\n",
    "    time.sleep(1)\n",
    "    \n",
    "    #获取数据\n",
    "    cpu = psutil.cpu_percent()\n",
    "    mem = psutil.virtual_memory()\n",
    "    t = time.strftime(\"%y-%m-%d %H:%M:%S\")\n",
    "    \n",
    "    #获取文件控制权-打开\n",
    "    f = open(\"大数据.txt\",mode=\"a\")\n",
    "    #写入\n",
    "    f.write(\"Time:\"+t+\"  CPU_Data:\"+str(cpu)+\"  MEM_Data:\"+str(mem[2])+\"\\n\")\n",
    "    #释放文件控制权-关闭\n",
    "    f.close()\n",
    "    \n",
    "    #输出\n",
    "    print(\"Time:\"+t+\"  CPU_Data:\"+str(cpu)+\"  MEM_Data:\"+str(mem[2]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4768543",
   "metadata": {},
   "source": [
    "r read 读取\n",
    "w write 覆盖写入\n",
    "a append 追加写入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ca28e0f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入库\n",
    "import psutil,time\n",
    "import requests as r \n",
    "\n",
    "#设置变量\n",
    "url = \"http://127.0.0.1:5000/?\"\n",
    "\n",
    "#循环\n",
    "for i in range(10):\n",
    "    #等待\n",
    "    time.sleep(1)\n",
    "    \n",
    "    #获取数据\n",
    "    cpu = psutil.cpu_percent()\n",
    "    t = time.strftime(\"%y-%m-%d %H:%M:%S\")\n",
    "    \n",
    "    #发送数据\n",
    "    r.get(url+str(cpu))\n",
    "\n",
    "    #获取文件控制权-打开\n",
    "    #f = open(\"大数据.txt\",mode=\"w\")\n",
    "    #写入\n",
    "    #f.write(\"Time\"+t+\"  Data:\"+str(cpu)+\"\\n\")\n",
    "    #释放文件控制权-关闭\n",
    "    #f.close()\n",
    "    \n",
    "    #输出\n",
    "    #print(\"Time:\"+t+\"  Data:\"+str(cpu))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "203115e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
